using System;
using System.IO;
using System.Data;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BEGalleryExtension
{
    [Themeable(true)]
    public class PhotoListControl : WebControl
    {
        #region Properties

        private int directoryID;
        public int DirectoryID
        {
            get { return directoryID; }
            set { directoryID = value; }
        }
	
        private int currentPage;
        public int CurrentPage
        {
            get { return currentPage; }
            set { currentPage = value; }
        }

        private int pageSize;
        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }

        private int repeatColumns;
        public int RepeatColumns
        {
            get { return repeatColumns; }
            set { repeatColumns = value; }
        }
	
        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            QueryString query = new QueryString();
            PhotoInformation pinfo = new PhotoInformation();
            DataTable table = pinfo.getPhotos(directoryID);

            PagerControl pager = new PagerControl();
            pager.ID = "topPager";
            pager.SetPagedDataSource(table.DefaultView, pageSize, currentPage);

            DataList list = new DataList();
            list.ID = "photoList";
            list.RepeatColumns = repeatColumns;
            list.RepeatLayout = RepeatLayout.Table;
            list.RepeatDirection = RepeatDirection.Horizontal;
            list.DataSource = pager.PagedData;
            list.CellSpacing = 5;

            PhotoListTemplate template = new PhotoListTemplate();
            list.ItemTemplate = template;
            list.DataBind();

            textRenderControl(pager, writer);
            textRenderControl(Constants.LineBreak, writer);
            textRenderControl(list, writer);
        }
        
        public void textRenderControl(Control ctl, HtmlTextWriter writer)
        {
            StringWriter sw = new StringWriter();
            ctl.RenderControl(new HtmlTextWriter(sw));
            writer.Write(sw.ToString());
            writer.Write(Environment.NewLine);
        }
    }

    public class PhotoListTemplate : ITemplate
    {
        public void InstantiateIn(Control container)
        {
            HyperLink link = new HyperLink();
            link.DataBinding += new EventHandler(link_DataBinding);

            container.Controls.Add(link);
        }

        void link_DataBinding(object sender, EventArgs e)
        {
            HyperLink link = (HyperLink)sender;
            DataListItem item = (DataListItem)link.NamingContainer;
            DataRowView drv = (DataRowView)item.DataItem;
            DataRow row = drv.Row;

            QueryString query = new QueryString();
            query["PhotoID"] = Convert.ToString(row["ID"]);

            link.ImageUrl = String.Format(Constants.PhotoHandler, row["ID"], "small");
            link.Text = Convert.ToString(row["FileName"]);
            link.NavigateUrl = query.All;
        }
    }
}
